package com.zzl.car.controller;

import com.github.pagehelper.PageInfo;
import com.zzl.car.context.ZdpJwtUserContext;
import com.zzl.car.entity.Illegal;
import com.zzl.car.entity.Notice;
import com.zzl.car.exception.ZdpException;
import com.zzl.car.service.IllegalService;
import com.zzl.car.util.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
public class zdpIllegalController {

    @Autowired
    private IllegalService illegalService;
    /*
    * 查询违章记录，生成违章
    * */
    @GetMapping("/getIllegal")
    public String getIllegal(){
        Integer size = illegalService.getIllegalAndHandler();
        return "开始查询"+size+"条记录";
    }

    // 获取所有违章信息
    @GetMapping("/getAllIllegal")
    public PageInfo<Illegal> getAllIllegal(@RequestParam(value = "name",required = false)String name,
                                      @RequestParam(value = "state",required = false)Integer state,
                                      @RequestParam(value = "currentPage",defaultValue = "1")int currentPage){

        return illegalService.getAllIllegal(name,state,currentPage);
    }

    @GetMapping("/noticeIllegal")
    public String noticeIllegal(){
        Integer integer = illegalService.sendEmailToIllegalUser();
        return "已通知"+integer.toString()+"名用户";
    }

    /*
    * 获得用户的违章记录
    * */
    @GetMapping("/getUserIllegal")
    public PageInfo<Illegal> getUserIllegal(@RequestParam(value="pageNum",defaultValue = "1")Integer pageNum,
                                        @RequestParam(value="state",defaultValue = "-1")Integer state){
        String telephone = ZdpJwtUserContext.getCurrentName();
        return illegalService.getIllegalByUserTelephoneAndState(telephone, state, pageNum, 4);
    }

    /*
    * 用户上传凭证
    * */
    @PostMapping("/uploadIllegalEvidence")
    public Illegal uploadIllegalEvidence(@RequestParam("files") MultipartFile[] images,
                                        @RequestParam("id") Integer id){
        // 看看值是不是被取到了
        System.out.println(images);
        System.out.println(id);
        Illegal illegal = null;
        try {
            illegal = illegalService.uploadIllegalEvidence(images, id);
        } catch (IOException e) {
            e.printStackTrace();
            throw new ZdpException(9999,"服务器繁忙，请稍后再试");
        }
        return illegal;
    }
}
